package sjtu.ist.warehouse4ws.action;

import java.io.FileNotFoundException;

import com.opensymphony.xwork2.ActionSupport;

import sjtu.ist.warehouse4ws.tool.GenerateWadl;
import sjtu.ist.warehouse4ws.tool.MyUtils;


import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.xml.sax.SAXException;
public class GenerateWadlAction extends ActionSupport{

private static final long serialVersionUID = 4532462611069475714L;
private GenerateWadl generateWadl;

String warnning;


public String getWarnning() {
	return warnning;
}
public void setWarnning(String warnning) {
	this.warnning = warnning;
}

public void setGenerateWadl(GenerateWadl generateWadl) {
	this.generateWadl = generateWadl;
}
public String genarateWadl() throws Exception{

	MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/wadl"));
	String basepath=ServletActionContext.getServletContext().getRealPath("/upload/files/domain/").replaceAll("\\\\", "/")+"/";
	//basepath is dynamic
	String outpath=ServletActionContext.getServletContext().getRealPath("/upload/files/wadl/").replaceAll("\\\\", "/")+"/";
	
	
	HttpServletRequest request = ServletActionContext.getRequest ();
	String xmlName=request.getParameter("nodeName");
	System.out.print(xmlName);
	String path=basepath+xmlName;
	String outFile=outpath+"Wadl"+xmlName;

	
			generateWadl.setDynamicPath(basepath);
			try {
				generateWadl.writeWadl(outFile, path);
				warnning="生成WADL文件成功！";
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				warnning="关联文件缺失，请添加相关文件！";
	
			}catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				warnning="关联文件缺失，请添加相关文件！";
	
			}

	return SUCCESS;
}
}
